#include "hud.h"

extern int G_W, G_H;

HUD::HUD(CASH *cash):QObject(), QGraphicsItemGroup()
{

    shuttle_type = 1;
    this->cash=cash;

    init();
}

void HUD::set_points(int value)
{
    if(value > 10){
        qDebug() << "error!!! value has been max 10 points";
        return;
    }

    if(value <= 0){
        item_hp->set_points(0);
        item_shield->set_points(0);

        item_ambient->damage();
        item_ambient->shuttle_destroyed();
    }

    if(value < 6){
        item_hp->set_points(value);
        item_shield->set_points(0);

        item_ambient->damage();

        item_triangle_mesh->shield_destroyed();

        return;
    }

    if(value >= 6 && item_hp->get_points() != 5){
        item_hp->set_points(5);
        value -= 5;

        item_shield->set_points(value);

        item_triangle_mesh->damage();

    }else{
        value -= 5;
        item_shield->set_points(value);

        item_triangle_mesh->damage();
    }

}

void HUD::add_score(int value)
{
    item_cpu->add_score(value);
}

QPainterPath HUD::shape() const
{
    return QPainterPath();
}

void HUD::set_shuttle_type(int type)
{
    switch (type) {
    case 1:
        item_type_defence->setVisible(true);
        item_type_damage->setVisible(false);
        item_type_agility->setVisible(false);
        break;

    case 2:
        item_type_defence->setVisible(false);
        item_type_damage->setVisible(true);
        item_type_agility->setVisible(false);
        break;

    case 3:
        item_type_defence->setVisible(false);
        item_type_damage->setVisible(false);
        item_type_agility->setVisible(true);
        break;

    case 0:
        item_type_defence->setVisible(false);
        item_type_damage->setVisible(false);
        item_type_agility->setVisible(false);
        break;

    default:
        break;
    }

    shuttle_type = type;
}

HUD::~HUD()
{
}

void HUD::init()
{
//hp items
    item_hp = new HUD_ITEM_HP(cash);

    this->addToGroup(item_hp);
    item_hp->setPos(0, 0);
    item_hp->setZValue(2);

//shield items
    item_shield = new HUD_ITEM_SHIELD(cash);

    this->addToGroup(item_shield);
    item_shield->setPos(0, 0);
    item_shield->setZValue(2);

//cpu items
    item_cpu = new HUD_ITEM_CPU(cash);

    this->addToGroup(item_cpu);
    item_cpu->setPos(0, 0);
    item_cpu->setZValue(2);

//    this->setPos(0, 0);
//    this->setZValue(5);

//ambient
    item_ambient = new HUD_ITEM_AMBIENT(cash);

    this->addToGroup(item_ambient);
    item_ambient->setPos(0, 0);
    item_ambient->setZValue(-2);

//mesh
    item_triangle_mesh = new HUD_ITEM_TRIANGLE_MESH(cash);

    this->addToGroup(item_triangle_mesh);
    item_triangle_mesh->setPos(0, 0);
    item_triangle_mesh->setZValue(-1);

//cannon indicator
    item_cannon = new HUD_ITEM_CANNON(cash);

    this->addToGroup(item_cannon);
    item_cannon->setPos(0, 0);
    item_cannon->setZValue(2);

    //

    item_type_defence = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/modes/defence.png"), this);
    item_type_defence->setPos(G_W - item_type_defence->boundingRect().width(), G_H/2 - item_type_defence->boundingRect().height()/2 - 200);

    item_type_damage = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/modes/damage.png"), this);
    item_type_damage->setPos(G_W - item_type_damage->boundingRect().width(), item_type_defence->pos().y() + item_type_defence->boundingRect().height());

    item_type_agility = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/modes/agility.png"), this);
    item_type_agility->setPos(G_W - item_type_agility->boundingRect().width(), item_type_damage->pos().y() + item_type_damage->boundingRect().height());


    set_shuttle_type(1);
}

void HUD::show_hud()
{
    item_hp->show_hud();
    item_shield->show_hud();
    item_cpu->show_hud();

    item_triangle_mesh->show();

    item_cannon->show_hud();

    set_shuttle_type(shuttle_type);
}

void HUD::hide_hud()
{
    item_hp->hide_hud();
    item_shield->hide_hud();
    item_cpu->hide_hud();

    item_triangle_mesh->hide();

    item_cannon->hide_hud();

    set_shuttle_type(0);
}

void HUD::show()
{
    this->item_hp->setVisible(true);
    this->item_shield->setVisible(true);
    this->item_cpu->setVisible(true);
    this->item_triangle_mesh->setVisible(true);

    this->item_cannon->setVisible(true);

    set_shuttle_type(shuttle_type);
}

void HUD::hide()
{
    this->item_hp->setVisible(false);
    this->item_shield->setVisible(false);
    this->item_cpu->setVisible(false);
    this->item_triangle_mesh->setVisible(false);

    this->item_cannon->setVisible(false);

    set_shuttle_type(0);
}


void HUD::slot_unvis_hud()
{
    item_hp->setVisible(false);
    item_shield->setVisible(false);
    item_triangle_mesh->setVisible(false);
    item_cpu->setVisible(false);

    this->item_cannon->setVisible(false);

    set_shuttle_type(0);
}

void HUD::slot_visible_hud()
{
    item_hp->setVisible(true);
    item_shield->setVisible(true);
    item_triangle_mesh->setVisible(true);
    item_cpu->setVisible(true);

    this->item_cannon->setVisible(true);

    set_shuttle_type(shuttle_type);
}

void HUD::slot_cannon_fire()
{
    item_cannon->slot_fire();
}
